<?php

/*
	[Discuz!] (C)2001-2009 Comsenz Inc111.
	This is NOT a freeware, use is subject to license terms

	$Id: home_magic.php 21217 2011-03-18 10:01:16Z monkey $
*/

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

if(!$_G['uid']) {
	showmessage('not_loggedin', NULL, array(), array('login' => 1));
}

if(!$_G['setting']['creditstransextra'][3]) {
	showmessage('credits_transaction_disabled');
} elseif(!$_G['setting']['magicstatus']) {
	showmessage('magics_close');
}

require_once libfile('function/magic');
loadcache('magics');

$_G['mnid'] = 'mn_common';
$magiclist = array();
$_G['tpp'] = 14;
$page = max(1, intval($_G['gp_page']));
$action = $_G['gp_action'];
$operation = $_G['gp_operation'];
$start_limit = ($page - 1) * $_G['tpp'];
$_G['gp_magicnum'] = empty($_G['gp_magicnum']) ? 1 :intval($_G['gp_magicnum']);

$comma = $typeadd = $filteradd = $forumperm = $targetgroupperm = '';
$magicarray = is_array($_G['cache']['magics']) ? $_G['cache']['magics'] : array();

if(!$_G['uid'] && ($operation || $action == 'mybox')) {
	showmessage('not_loggedin', NULL, array(), array('login' => 1));
}

$location = 0;

if(empty($action) && !empty($_G['gp_mid'])) {
	$_G['gp_magicid'] = DB::result_first("SELECT m.magicid FROM ".DB::table('common_magic')." m WHERE m.identifier='$_G[gp_mid]'");
	if($_G['gp_magicid']) {
		$action = 'mybox';
		$operation = 'use';
	} else {
		$action = 'mybox';
		$operation = 'use';
		$location = 1;
	}
}

$action = empty($action) ? 'mybox' : $action;
$actives[$action] = ' class="a"';

if($action == 'mybox') {
	
	$magicid = $_G['gp_magicid'];
	
	$magic = DB::fetch_first("SELECT m.*
			FROM ".DB::table('common_magic')." m
			WHERE m.magicid='$magicid'");
	if(!$magic) {
		showmessage('magics_nonexistence');
	}
	$magicperm = unserialize($magic['magicperm']);
	$magic['pic'] = strtolower($magic['identifier']).'.gif';

	if($operation == 'use') {

		if(!@include_once DISCUZ_ROOT.($magicfile = "./source/class/magic/magic_$magic[identifier].php")) {
			showmessage('magics_filename_nonexistence', '', array('file' => $magicfile));
		}
		$magicclass = 'magic_'.$magic['identifier'];
		$magicclass = new $magicclass;
		$magicclass->magic = $magic;
		$magicclass->parameters = $magicperm;
		$useperoid = magic_peroid($magic, $_G['uid']);

		if(submitcheck('usesubmit')) {
			if($useperoid !== true && $useperoid <= 0) {
				showmessage('magics_outofperoid_'.$magic['useperoid'], '', array('usenum' => $magic['usenum']));
			}
			$total = 0;
			$total = $_G['gp_magicnum'] * $magic['price'];

			if($total > getuserprofile('extcredits'.$magic['credit'])) {
				//积分不够
				showmessage('trade_credit_lack');
			}
			$a = 'extcredits'.$magic['credit'];

			if(method_exists($magicclass, 'usesubmit')) {
				
				$isad = 0;
				if($_G['gp_mid'] == 'stick') {
					$isad = 1;
					$act = 'PUA';
				} elseif($_G['gp_mid'] == 'jack') {
					$isad = 2;
					$act = 'PUB';
				} elseif($_G['gp_mid'] == 'highlight') {
					$isad = 3;
					$act = 'PUC';
				} else {
					showmessage('undefined_action');
				}
				
				if($_G['gp_tid'] && $isad && $total) {

					DB::query("UPDATE ".DB::table('forum_thread')." SET isad='$isad' WHERE tid='$_G[gp_tid]' AND authorid='$_G[uid]' LIMIT 1");

					DB::query("UPDATE ".DB::table('common_member_count')." SET $a=$a+(-'$total') WHERE uid='$_G[uid]'");

					updatemembercount($_G['uid'], array($magic['credit'] => -$total), true, $act, $_G['gp_tid']);

					//用于改变限时帖子有效期
					$_G['gp_pushad'] = 1;

					$magicclass->usesubmit();

				} else {
					showmessage('undefined_action');
				}
				
			}
			dexit();
		}

		include template('home/space_pushad_mybox_opreation');
		dexit();

	} else {
		showmessage('undefined_action');
	}

} elseif($action == 'log') {

	$subactives[$operation] = 'class="a"';
	$loglist = array();
	if($operation == 'uselog') {
		$query = DB::query("SELECT COUNT(*) FROM ".DB::table('common_magiclog')." WHERE uid='$_G[uid]' AND action='2'");
		$multipage = multi(DB::result($query, 0), $_G['tpp'], $page, 'home.php?mod=magic&action=log&amp;operation=uselog');

		$query = DB::query("SELECT ml.*, me.username FROM ".DB::table('common_magiclog')." ml
			LEFT JOIN ".DB::table('common_member')." me ON me.uid=ml.uid
			WHERE ml.action='2' AND ml.uid='$_G[uid]' ORDER BY ml.dateline DESC
			LIMIT $start_limit, $_G[tpp]");
		while($log = DB::fetch($query)) {
			$log['dateline'] = dgmdate($log['dateline'], 'u');
			$log['name'] = $magicarray[$log['magicid']]['name'];
			$loglist[] = $log;
		}

	} elseif($operation == 'buylog') {
		$query = DB::query("SELECT COUNT(*) FROM ".DB::table('common_magiclog')." WHERE uid='$_G[uid]' AND action='1'");
		$multipage = multi(DB::result($query, 0), $_G['tpp'], $page, 'home.php?mod=magic&action=log&amp;operation=buylog');

		$query = DB::query("SELECT * FROM ".DB::table('common_magiclog')."
			WHERE uid='$_G[uid]' AND action='1' ORDER BY dateline DESC
			LIMIT $start_limit, $_G[tpp]");
		while($log = DB::fetch($query)) {
			$log['credit'] = $log['credit'] ? $log['credit'] : $_G['setting']['creditstransextra'][3];
			$log['dateline'] = dgmdate($log['dateline'], 'u');
			$log['name'] = $magicarray[$log['magicid']]['name'];
			$loglist[] = $log;
		}

	} elseif($operation == 'givelog') {
		$query = DB::query("SELECT COUNT(*) FROM ".DB::table('common_magiclog')." WHERE uid='$_G[uid]' AND action='3'");
		$multipage = multi(DB::result($query, 0), $_G['tpp'], $page, 'home.php?mod=magic&action=log&amp;operation=givelog');

		$query = DB::query("SELECT ml.*, me.username FROM ".DB::table('common_magiclog')." ml
			LEFT JOIN ".DB::table('common_member')." me ON me.uid=ml.targetuid
			WHERE ml.uid='$_G[uid]' AND ml.action='3' ORDER BY ml.dateline DESC
			LIMIT $start_limit, $_G[tpp]");
		while($log = DB::fetch($query)) {
			$log['dateline'] = dgmdate($log['dateline'], 'u');
			$log['name'] = $magicarray[$log['magicid']]['name'];
			$loglist[] = $log;
		}

	} elseif($operation == 'receivelog') {
		$query = DB::query("SELECT COUNT(*) FROM ".DB::table('common_magiclog')." WHERE targetuid='$_G[uid]' AND action='3'");
		$multipage = multi(DB::result($query, 0), $_G['tpp'], $page, 'home.php?mod=magic&action=log&amp;operation=receivelog');

		$query = DB::query("SELECT ml.*, me.username FROM ".DB::table('common_magiclog')." ml
			LEFT JOIN ".DB::table('common_member')." me ON me.uid=ml.uid
			WHERE ml.targetuid='$_G[uid]' AND ml.action='3' ORDER BY ml.dateline DESC
			LIMIT $start_limit, $_G[tpp]");
		while($log = DB::fetch($query)) {
			$log['dateline'] = dgmdate($log['dateline'], 'u');
			$log['name'] = $magicarray[$log['magicid']]['name'];
			$loglist[] = $log;
		}

	}
	$navtitle = lang('core', 'title_magics_log');

} else {
	showmessage('undefined_action');
}

include template('home/space_pushad');

?>